import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

export default {
    data: {
        title: ""
    },
    onInit() {
        this.title = "Hello World";
    },

    // 设置播放器回调函数
    setCallBack(audioPlayer) {
        console.info('audio111111111111');
        audioPlayer.on('dataLoad', () => {              //设置'dataLoad'事件回调，src属性设置成功后，触发此回调
            console.info('audio set source success');
            audioPlayer.play();                           //调用play方法开始播放，触发'play'事件回调
        });
        audioPlayer.on('play', () => {                  //设置'play'事件回调
            console.info('audio play success');
        });
        audioPlayer.on('finish', () => {                //设置'finish'事件回调，播放完成触发
            console.info('audio play finish');
            audioPlayer.release();                        //audioPlayer资源被销毁
            audioPlayer = undefined;
        });
        audioPlayer.on('error', (error) => {             //设置'error'事件回调
            console.info(`audio error called, errName is ${error.name}`);
            console.info(`audio error called, errCode is ${error.code}`);
            console.info(`audio error called, errMessage is ${error.message}`);
        });
    },

    async audioPlayerDemo() {
        let audioPlayer = media.createAudioPlayer();     //创建一个音频播放实例
        this.setCallBack(audioPlayer);                   //设置事件回调
        //        let fdPath = 'fd://'
        // path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" 命令，将其推送到设备上
        //        let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile/01.mp3';

//        网络音乐
//        let fdPath = 'http://music.163.com/song/media/outer/url?id=317151.mp3'
//hdc file send F:\Maker\static\countdown.mp3 /data/local/tmp

//        本地音乐
        let fdPath = 'fd://'
        let path = '/data/local/tmp/countdown.mp3'

        await fileIO.open(path).then((fdNumber) => {
            fdPath = fdPath + '' + fdNumber;
            console.info('open fd success fd is' + fdPath);
        }, (err) => {
            console.info('open fd failed err is' + err);
        }).catch((err) => {
            console.info('open fd failed err is' + err);
        });

        audioPlayer.src = fdPath; //设置src属性，并触发'dataLoad'事件回调
    }

}